package com.hns.can.service.web;

import com.hns.can.service.service.DriverExamService;
import com.hns.can.service.service.DriverRankService;
import com.hns.can.service.vo.DriverRankVo;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.web.BaseController;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * 司机总评分Controller
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/driverRank")
public class DriverRankController extends BaseController {

    final private String[] headerNames = {"排名","整体得分","驾驶员","所属机构","运行里程(KM)","运行时长","情绪得分","安全得分","安保得分","节能得分" };
    final private String[] headerFields = new String[]{"allRank", "totalGrade","drvName", "organName", "totalMile", "lastingTime", "moodGrade", "safeGrade", "securityGrade", "energyGrade" };


    @Autowired
    private DriverRankService driverRankService;

    @Autowired
    private DriverExamService driverExamService;

    /**
     * 分页查询驾驶员总评分
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchDriverRank")
    @ResponseBody
    public Object searchDriverRank(CommonParamsVo vo) throws Exception {
        driverExamService.getReportDate(vo.getBeginTime(), vo.getReportType(), vo);
        Pagination page = driverRankService.searchDriverRank(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 导出驾驶员总评分Excel
     * @param vo
     * @param response
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportDriverRankExcel")
    public void exportDriverRankExcel(CommonParamsVo vo,HttpServletResponse response) throws Exception {
        List<DriverRankVo> list = getDriverRankFileData(vo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "excel", URLEncoder.encode("驾驶员总评分", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶员总评分数据");
    }

    /**
     * 导出驾驶员总评分Pdf
     * @param vo
     * @param response
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportDriverRankPdf")
    public void exportDriverRankPdf(CommonParamsVo vo,HttpServletResponse response) throws Exception {
        List<DriverRankVo> list = getDriverRankFileData(vo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "pdf", URLEncoder.encode("驾驶员总评分", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶员总评分数据");

    }

    /**
     * 获取驾驶员总评分数据
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    private List<DriverRankVo> getDriverRankFileData(CommonParamsVo vo)throws Exception{
        driverExamService.getReportDate(vo.getBeginTime(), vo.getReportType(),vo);
        List<DriverRankVo> list = driverRankService.getAllDriverRank(vo);
        if (PubUtil.isEmpty(list)) {
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }
}
